<?php

namespace app\admin\controller;

use app\common\model\ChangguanFeiyong;
use app\common\model\Changguan;
use app\common\model\ChangguanJinyong;

class ChangguanShiduan extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 列表
     * @return \think\response\Json
     */
    public function list()
    {

        //查询条件
        $where = array();

        if (input('name')) {
            $where['name'] = array('like', "%" . input('name') . "%");
        }

        //分页信息
        $page = max(input('page'), 1);

        $size = input('limit', 20);

        $list = Changguan::getPage($where, $page, $size);

        //总页码
        $total = Changguan::getCount($where);


        //数据返回
        if (!$list) {
            return returnJson(0, '', array('list' => [], 'total' => 0));
        }
        foreach ($list as &$v) {
            $w = array(
                'cgid' => $v['id'],
                'status' => 1
            );
            $v['gongzuori'] = ChangguanFeiyong::getAll($w, 'start_time asc');

            $w['status'] = 2;
            $v['jiejiari'] = ChangguanFeiyong::getAll($w, 'start_time asc');
        }

        return returnJson(0, '', [
            'list' => $list,
            'total' => $total
        ]);
    }

    /**
     * 新增、编辑
     * @return \think\response\Json
     */
    public function edit()
    {

        if (request()->isPost()) {

            $price = input('price/a');
            $save = [];
            foreach ($price as $k => $v) {
                $save[] = array(
                    'cgid' => input('cgid'),
                    'status' => input('status'),
                    'price' => $v['price'],
                    'start_time' => $v['start_time'],
                    'end_time' => $v['end_time'],
                    'create_time' => time(),
                    'update_time' => time()
                );
            }
            ChangguanFeiyong::setDel(array('cgid' => input('cgid'), 'status' => input('status')));
            ChangguanFeiyong::setSaveAll($save);
            return returnJson(0, 'success');
        }
        //返回
        return returnJson(0, '');
    }


    /**
     * 列表
     * @return \think\response\Json
     */
    public function detail()
    {

        //查询条件
        $where = array();

        if (input('cgid', 0)) {
            $where['cgid'] = array('eq', input('cgid'));
        }
        if (input('status', 0)) {
            $where['status'] = array('eq', input('status'));
        }
        $time = [
            ['start_time' => '00:00', 'end_time' => '01:00', 'price' => ''],
            ['start_time' => '01:00', 'end_time' => '02:00', 'price' => ''],
            ['start_time' => '02:00', 'end_time' => '03:00', 'price' => ''],
            ['start_time' => '03:00', 'end_time' => '04:00', 'price' => ''],
            ['start_time' => '04:00', 'end_time' => '05:00', 'price' => ''],
            ['start_time' => '05:00', 'end_time' => '06:00', 'price' => ''],
            ['start_time' => '06:00', 'end_time' => '07:00', 'price' => ''],
            ['start_time' => '07:00', 'end_time' => '08:00', 'price' => ''],
            ['start_time' => '08:00', 'end_time' => '09:00', 'price' => ''],
            ['start_time' => '09:00', 'end_time' => '10:00', 'price' => ''],
            ['start_time' => '10:00', 'end_time' => '11:00', 'price' => ''],
            ['start_time' => '11:00', 'end_time' => '12:00', 'price' => ''],
            ['start_time' => '12:00', 'end_time' => '13:00', 'price' => ''],
            ['start_time' => '13:00', 'end_time' => '14:00', 'price' => ''],
            ['start_time' => '14:00', 'end_time' => '15:00', 'price' => ''],
            ['start_time' => '15:00', 'end_time' => '16:00', 'price' => ''],
            ['start_time' => '16:00', 'end_time' => '17:00', 'price' => ''],
            ['start_time' => '17:00', 'end_time' => '18:00', 'price' => ''],
            ['start_time' => '18:00', 'end_time' => '19:00', 'price' => ''],
            ['start_time' => '19:00', 'end_time' => '20:00', 'price' => ''],
            ['start_time' => '20:00', 'end_time' => '21:00', 'price' => ''],
            ['start_time' => '21:00', 'end_time' => '22:00', 'price' => ''],
            ['start_time' => '22:00', 'end_time' => '23:00', 'price' => ''],
            ['start_time' => '23:00', 'end_time' => '24:00', 'price' => ''],
        ];
        $detail = ChangguanFeiyong::getAll($where, 'start_time asc', 'start_time,end_time,price');
        if ($detail) {
            foreach ($detail as &$v) {
                $key = (int)substr($v['start_time'], 0, 2);
                $time[$key]['price'] = $v['price'];
            }
        }
        return returnJson(0, '', $time);
    }

    /**
     * 列表
     * @return \think\response\Json
     */
    public function jinyonglist()
    {

        //查询条件
        $where = array();

        if (input('cgid')) {
            $where['cgid'] = array('eq', input('cgid'));
        }
        if (input('status')) {
            $where['status'] = array('eq', input('status'));
        }

        //分页信息
        $page = max(input('page'), 1);
        $size = input('limit', 20);

        $list = ChangguanJinyong::getPage($where, $page, $size);

        //总页码
        $total = ChangguanJinyong::getCount($where);

        //数据返回
        if (!$list) {
            return returnJson(0, '', array('list' => [], 'total' => 0));
        }

        $all_cg = Changguan::getColumn([], 'id,name');
        foreach ($list as &$v) {
            $v['name'] = isset($all_cg[$v['cgid']]) ? $all_cg[$v['cgid']] : '';
        }

        return returnJson(0, '', [
            'list' => $list,
            'total' => $total
        ]);
    }


    /**
     * 列表
     * @return \think\response\Json
     */
    public function jinyongdetail()
    {

        //查询条件
        $where = array();

        if (input('cgid', 0)) {
            $where['cgid'] = array('eq', input('cgid'));
        }

        if (input('status')) {
            $where['status'] = input('status');
        }

        if (input('date')) {
            $where['date'] = input('date');
        }

        if (input('week')) {
            $where['week'] = input('week');
        }

        $detail = ChangguanJinyong::getColumn($where, 'start_time');

        return returnJson(0, '', $detail);
    }


    /**
     * 禁用编辑
     * @return \think\response\Json
     */
    public function jinyongedit()
    {

        if (request()->isPost()) {

            $start_time = input('start_time/a');
            $save = [];
            foreach ($start_time as $k => $v) {
                $end = (int)substr($v, 0, 2) + 1;
                $end_time = $end < 10 ? '0' . $end . ':00' : $end . ':00';
                $save[] = array(
                    'cgid' => input('cgid'),
                    'status' => input('status'),
                    'date' => input('date'),
                    'week' => input('week'),
                    'start_time' => $v,
                    'end_time' => $end_time,
                    'create_time' => time(),
                    'update_time' => time()
                );
            }

            $where = array(
                'cgid' => input('cgid'),
                'status' => input('status')
            );
            if (input('status') == 1) {
                $where['week'] = input('week');
            } else if (input('status') == 2) {

                $where['date'] = input('date');
            }
            ChangguanJinyong::setDel($where);
            ChangguanJinyong::setSaveAll($save);
            return returnJson(0, 'success');
        }
        //返回
        return returnJson(0, '');
    }

    /**
     * 删除禁用时段
     * @return \think\response\Json
     */
    public function delete()
    {
        if (input('id', 0)) {
            //查询条件
            $where['id'] = input('id');

            //删除记录
            ChangguanJinyong::setDel($where);
        }
        return returnJson(0, '');

    }

}
